package com.example.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 切面类，定义通知
 */
public class MyAspect {
    /**
     * 前置通知
     *
     * @param joinPoint 切入点
     */
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("beforeAdvice:" + joinPoint.getSignature().getName() + "执行前");
    }

    /**
     * 最终通知
     *
     * @param joinPoint 切入点
     */
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("afterAdvice:" + joinPoint.getSignature().getName() + "执行完成");
    }

    /**
     * 后置通知
     * @param joinPoint 切入点
     */
    public void afterReturningAdvice(JoinPoint joinPoint) {
        System.out.println("afterReturningAdvice:"+joinPoint.getSignature().getName() + "执行完成");
    }

    /**
     * 异常通知
     * @param joinPoint 切入点
     * @param throwable 抛出的异常
     */
    public void afterThrowingAdvice(JoinPoint joinPoint, Throwable throwable) {
        System.out.println("afterThrowingAdvice" + joinPoint.getSignature().getName() + "产生了异常: " + throwable.getMessage());
    }

    /**
     *  环绕通知
     * @param proceedingJoinPoint 切入点
     * @return 返回方法实际的返回值
     * @throws Throwable 抛出的异常
     */
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知开始");
        Object[] args = proceedingJoinPoint.getArgs();
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕通知结束");
        return proceed;
    }
}
